<?php
/**
 * 会员信息信息控制器
 * @author IIIThSoul
 *
 */
namespace Manage\Controller;

class MemberController extends ManageController
{
	/*会员列表页*/
	function index ()
	{
		$member = M ('jjlg_member');
		$where = array ();
		if ($_POST) {
			/*查询参数*/
			//
		}
		/* 分页 */
		$count = $member->where ($where)->count ();
		$page = new \Think\Page($count, 10);
		$show = $page->show ();
		$this->assign ('nowpage', $page->nowpage ());
		$this->assign ('pagestr', $show);
		$this->assign ('count', $count);
		/*数据展示*/
		$data = $member->field ("id, username, reg_time, reg_ip")
			->where ($where)
			->limit ($page->firstRow . ',' . $page->listRows)
			->select ();
		/*数据解密*/

		$this->assign ('data', $data);
		$this->display ();
	}

	/*会员修改展示页*/
	function edit ($id)
	{
		$member = M ('jjlg_member');
		$where = array ();
		if ( !IS_POST) {//显示查询结果
			$data = $member->alias ('m')
				->field ("m.id,m.username,m.balance,m.total_score,m.score_balance,m.coupon_balace,m.email,m.is_email_valid,m.level_id,m.gener,m.birthday,m.crm_user_id,m.parent_id,m.domain_id,m.realname,m.mobile,m.is_mobile_valid,d.title,c.channel_name,l.name")
				->join (" LEFT JOIN jjlg_domain d ON d.id=m.domain_id")
				->join (" LEFT JOIN jjlg_channel c ON c.id=m.parent_id")
				->join (" LEFT JOIN jjlg_member_level l ON l.id=m.level_id")
				->where ('m.id =' . $id)
				->find ();
			$data['email'] = rsa_decrypt($data['email']);
			$data['realname'] = rsa_decrypt($data['realname']);
			$data['mobile'] = rsa_decrypt($data['mobile']);
			$this->assign ('data', $data);
			$this->display ('form');
		} else {//显示修改提交的结果
			if(I('post.email')){
				if(!filter_var(I('post.email'), FILTER_VALIDATE_EMAIL)) $this->error('邮箱不合法，请认真填写!',U('/Member/edit/id/'.$id));
				$data = array("email" => rsa_encrypt(I('post.email')));
			}
			if(!I('post.realname')) $this->error('真实姓名不能为空!',U('/Member/edit/id/'.$id));
			if(!preg_match("/^1[34578]\d{9}$/", I('post.mobile'))) $this->error('手机号不合法，请认真填写!',U('/Member/edit/id/'.$id));
			$data['gener'] = I('post.gener');
			$data['realname'] = rsa_encrypt(I('post.realname'));
			$data['mobile'] = rsa_encrypt(I('post.mobile'));
			$state = $member->where('id=' . $id)->save($data);
			if(false !== $state)
			{
				$this->success('会员信息更新成功！',U('/Member/index'));
			}
			else
			{
				$this->error('会员信息更新失败!',U('/Member/edit/id/'.$id));
			}
		}
	}

}